package android.pot.com.smartcar.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.pot.com.smartcar.MainActivity;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.config.MyConfig;
import android.pot.com.smartcar.fragment.XieyiDialogView;
import android.pot.com.smartcar.util.ImageLoadUtils;
import android.pot.com.smartcar.util.StartupUtils;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;



public class StartupPageActivity extends FragmentActivity {
	
	private static final int SPLASH_DISPLAY_LENGHT = 2000; // 延迟1秒 1000
	public static final Class<?> HOME_CLASS = MainActivity.class;

	private ImageView iv_img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		setSwipeBackEnable(false);
		setContentView(R.layout.activity_startup_page);
		setImgTransparent(this);
		initView();
		initData();
	}

	public static void setImgTransparent(Activity activity){
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = activity.getWindow();
			window.clearFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
							| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  //不隐藏和透明虚拟导航栏  因为会遮盖底部的布局
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE//保持布局状态
			);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);//不隐藏和透明虚拟导航栏  因为会遮盖底部的布局

		}
	}

	protected void initView() {
		iv_img = findViewById(R.id.iv_img);

	}



	private void startHomePage() {

		if (StartupUtils.isFirstStartUp(StartupPageActivity.this)){
			XieyiDialogView dialogView = new XieyiDialogView(StartupPageActivity.this);
			dialogView.show(getSupportFragmentManager(),"dialogView");
			dialogView.setMethodClickListener(new XieyiDialogView.OnMethodClickListener() {
				@Override
				public void methodClick() {
					StartupUtils.setFirstStartUp(StartupPageActivity.this);
					Intent intent = new Intent(StartupPageActivity.this, HOME_CLASS);
					startActivity(intent);
					finish();
				}
			});
		}else{
			StartupUtils.setFirstStartUp(StartupPageActivity.this);
			Intent intent = new Intent(StartupPageActivity.this, HOME_CLASS);
			startActivity(intent);
			finish();
		}

	}

	@Override
	public void onStart() {
		super.onStart();

	}



	protected void initData(){
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				getImg();
			}
		}, 1500);
	}
	private void getImg(){
		Map<String, String> params = new HashMap<>();
		Action.get().start_page_ad(StartupPageActivity.this, params, new MyResponseHandler() {
			@Override
			public void onSuccess(String message,JSONObject response) {
				try {
					String data = response.getString("data");
					ImageLoadUtils.INSTANCE.loadOptionImageView(iv_img, MyConfig.IMG_URL+data,R.drawable.launcher);
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							startHomePage();
						}
					}, SPLASH_DISPLAY_LENGHT);
				} catch (JSONException e) {
					e.printStackTrace();
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							startHomePage();
						}
					}, SPLASH_DISPLAY_LENGHT);
				}
			}
		});

	}

}
